<template>
	<view class="container">
		<view class="goodslist" v-if="goodslist.length>0">
			<view class="body_bottom_2" v-for="(item,index) in goodslist" :key="index">
				<image class="goods_img" @tap="goDetail(item.goods_id)" :src="item.goods_img"/>
				<view class="body_bottom_2_item">
					<text class="body_bottom_2_text1">{{item.goods_name}}</text>
					<view class="body_bottom_2_flex">
						<view class="body_bottom_2_flex_tit">￥<text>{{item.goods_price}}</text></view>
						<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/goods/shoucang2.png" class="small-img" @click="Cancelcollection(item.goods_id,index)"/>
					</view>
				</view>
			</view>
			<u-loadmore :status="hasMore?'loading':'nomore'" />
		</view>
		<view class="empty-container" v-else>
			<u-empty text="暂无收藏商品" :icon="emptyIcon">
				<button @tap='goIndex' size="mini" class="chooseBtn">去逛逛</button>
			</u-empty>
		</view>
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				emptyIcon:'https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/empty.png',
				goodslist:[],
				pageIndex:1,
				hasMore:true
			}
		},
		onLoad() {
			_this=this
			this.GoodsList()	
		},
		onReachBottom() {
			if(this.hasMore){
				this.pageIndex++
				this.GoodsList()
			}
		},
		methods: {
			//跳转到商品详情
			goDetail(goods_id){
				uni.navigateTo({
					url:`/pages/goods/goods-detail?id=${goods_id}`
				})
			},
			//获取收藏商品列表
			async GoodsList(){
				const params = {
					pageIndex:this.pageIndex,
					pageSize:20,
				}
				const data = await _this.$post('api/CollectList',params)
				this.goodslist.push(...data.data.collect)
				this.hasMore=data.data.collect.length>=20
			},
			//取消收藏
			async Cancelcollection(id,index){
				const params = {
					goods_id:id,
					type: 2
				}
				const data = await _this.$post('api/EditCollect',params)
				if(data.code==200){
					this.$u.toast(data.msg)
					this.goodslist.splice(index,1)
				}else{
					this.$u.toast(data.msg)
				}
			},
			//加入购物车
			async addcart(id) {
				const params = {
					goods_id:id,
					item_id:0,
					goods_num:1
				}
				const data  = await AddCart(params)
				if(data.code==200){
					this.$u.toast(data.msg)
				}else{
					this.$u.toast(data.msg)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		padding: 20rpx 20rpx 0 20rpx;
		.body_bottom_2 {
			display: flex;
			padding: 30rpx;
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			background: #FFFFFF;
			.goods_img {
				width: 140rpx;
				height: 140rpx;
				border-radius: 10rpx;
			}
			.body_bottom_2_item{
				flex: 1; 
				display: flex; 
				flex-direction: column;
				margin-left: 20rpx;
				justify-content: space-between;
				padding: 10rpx 0;
				.body_bottom_2_text1{
					font-size: 28rpx;
				}
				.body_bottom_2_flex{
					width: 100%;
					display: flex;
					align-items: center;
					.body_bottom_2_flex_tit{
						color:$theme-F94A36;
						flex: 1;
						font-size: 26rpx;
						text{
							font-size: 30rpx;
							font-weight: 500;
						}
					}
				}
			}
			.small-img{
				width: 36rpx;
				height: 36rpx;
				&:not(:last-child){
					margin-right: 10rpx;
				}
			}
		}
	}
	.u-empty-container{
		padding-top: 20%;
		.chooseBtn{
			color: #fff;
			margin-top: 20rpx;
			background: $theme-color;
		}
	}
</style>
